package com.hongning.railway.ui.main.mine.experience;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;

import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqAddExperience;
import com.hongning.railway.entity.reqbean.ReqAddScoreBean;
import com.hongning.railway.entity.reqbean.ReqUpdateExperience;
import com.hongning.railway.entity.resbean.ResExperienceBean;
import com.hongning.railway.util.Apputils;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ExperiencePubOrEditFragmentViewModel extends BaseViewModel<AppRepository> {
    public ObservableField<String> title = new ObservableField<>();
    public ObservableField<String> content = new ObservableField<>();
    public ExperiencePubOrEditFragmentViewModel(@NonNull Application application) {
        super(application);
    }

    public ExperiencePubOrEditFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void setData(ResExperienceBean.SelectResultBean data) {
        title.set(data.getExperiencetitle());
        content.set(data.getExperiencecontent());
    }

    public void pub(){
        ReqAddExperience reqAddExperience = new ReqAddExperience();
        reqAddExperience.setExperienceTitle(title.get());
        reqAddExperience.setExperienceContent(content.get());
        reqAddExperience.setUserId(Apputils.getUserId());
        model.addExperience(reqAddExperience)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .subscribe(new ApiDisposableObserver<Boolean>() {
                    @Override
                    public void onResult(Boolean aBoolean) {
                        if (aBoolean){
                            ReqAddScoreBean reqAddScoreBean = new ReqAddScoreBean();
                            reqAddScoreBean.setProjectId("001001001004");
                            Messenger.getDefault().send(reqAddScoreBean, Constant.ADD_SCORE);
                            ToastUtils.showShort("发布成功");
                            finish();
                        }
                    }
                });
    }

    public void update(String id){
        ReqUpdateExperience reqAddExperience = new ReqUpdateExperience();
        reqAddExperience.setExperienceTitle(title.get());
        reqAddExperience.setExperienceContent(content.get());
        reqAddExperience.setId(id);
        model.updateExperience(reqAddExperience)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .subscribe(new ApiDisposableObserver<Boolean>() {
                    @Override
                    public void onResult(Boolean aBoolean) {
                        if (aBoolean){
                            ToastUtils.showShort("修改成功");
                            finish();
                        }
                    }
                });
    }
}
